<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 分组模型
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class GroupModel extends CommonModel{

	/**
     * 返回对象的定义
     */
    static public function init__(){

        return array(

            'table_name' => 'group',

            'props' => array(

                // 主键应该是只读，确保领域对象的“不变量”
                'group_id' => array( 'readonly' => true ),
            ),

            'attr_protected' => 'group_id',

            'check' => array(

                 'group_name'=>array(

                     array( 'require',G::L( '组名不能为空！' ) ),
    	             array( 'number_underline_english',G::L( '组名只能是由数字，下划线，字母组成' ) ),
    	             array( 'groupName', G::L( '组名已经存在' ) ,'condition'=>'must','extend'=>'callback' ),
                 ),

                 'group_title'=>array(

                     array( 'require',G::L( '组显示名不能为空！' ) ),
                 ),

                 'group_sort'=>array(

                     array( 'group_sort','number',G::L( '序号只能是数字' ),'condition'=>'notempty','extend'=>'regex' ),
                 ),
             ),

        );

    }


	/**
     * 开启一个查询，查找符合条件的对象或对象集合
     */
    static function F( ){

        $arrArgs = func_get_args();
        return ModelMeta::instance( __CLASS__ )->findByArgs( $arrArgs );
    }


    /**
     * 返回当前 Model 类的元数据对象
     */
    static function M( ){

        return ModelMeta::instance(__CLASS__);
    }


    /**
     * 角色分组名字唯一性验证
     */
    public function groupName(){

    	$nId = Safe::getGpc( 'id','P' );
    	$sGroupName = Safe::getGpc( 'group_name','P' );

    	$sGroupInfo = '';

    	if( $nId ){

    		$arrGroup = self::F( 'group_id =?',$nId )->asArray()->getOne( );
    		$sGroupInfo = trim( $arrGroup['group_name'] );
    	}

    	if( $sGroupName != $sGroupInfo ){

    		$arrResult = self::F()->getBygroup_name( $sGroupName )->toArray();

    	    if( !empty( $arrResult['group_id'] ) ) {

        	   return false;
            }

            else{

			   return true;
		    }

    	}

    	return true;
    }

}
